<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class GoodsController extends AdminBaseController {
    public function __construct(){
        parent::__construct();
        if(empty($_SESSION['user']['id'])){
	    	$this->error('未登录,非法访问',U('Admin/Index/admin'),0);
	    }
    }
    public function goods(){
        $find=I('request.key');
        $map['goods_name']=array('LIKE',"%$find%");
        $User = M('goods_brand'); // 实例化User对象
        $count= $User->where("goods_name like '%{$find}%' and status=1")->count();// 查询满足要求的总记录数
        $Page= new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(6)
        foreach($map as $key=>$val) {   
         $Page->parameter[$key]   =   $find;
         }
        $show = $Page->show();
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->where("goods_name like '%{$find}%' and status=1")->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('key',$find);
        $this->assign('goods',$list);
        $this->display();
    }
    //自动完成
    public function autokey(){
        $key=I('term');
        $res=M('goods')->field('goods_name')->where(array('goods_name'=>array('like',"%{$key}%")))->select();
        $str="[";
        foreach ($res as $v){
            $str.='"'.$v['goods_name'].'"'.",";
        }
        $str=rtrim($str,',');
        $str.="]";
        echo $str ;
    }
    public function goodsdel(){
        $re= M('goods')->where(array("id"=>I('get.id')))->setField(array('status'=>0));
         $arr=array();
        if($re){
           
            $arr['status']=1;
            $arr['msg']='删除成功';
             
        }else{
            $arr['msg']='删除失败';
        }
        $this->ajaxReturn($arr);
    }
    //批量删除
    public function doDel_all(){
    
        foreach (I('get.row') as $v){
           $re= M('goods')->where("id={$v}")->setField(array('status'=>0));
        }
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='批量删除成功';
             
        }else{
            $arr['msg']='批量删除失败';
        }
        $this->ajaxReturn($arr);
         
    }
    //商品总属性表
    public function attr(){
        $pagesize=3;
        $counts=M('attr_type')->count();
        $pagetotal=ceil($counts/$pagesize);
        $data=M('type')->select();
        $this->assign('types',$data);
        $this->assign('pagetotal',$pagetotal);
        $this->display();
    }
    public function doattr(){
        $data=M('attr_type')->page(I('get.p'),3)->select();
        $this->ajaxReturn($data);
    }
    public function doattradd(){
        $data=I('get.');
        $arr=array();
        foreach ($data as $K=>$v){
            if($v==''){
                $arr['status']=0;
                $arr['msg']='数据不能为空';
                $this->ajaxReturn($arr);
                exit();
            }
        }
    $re=M('attr')->filter('strip_tags')->add($data);
          if($re){
            $arr['status']=1;
            $arr['msg']='添加成功';
        }else{
            $arr['status']=0;
            $arr['msg']='添加失败';
        }
        $this->ajaxReturn($arr);  
    }
    public function delattr(){
       $re=M('attr')->where(array('id'=>I('get.id')))->delete();
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='删除成功';
        }else{
            $arr['status']=0;
            $arr['msg']='删除失败';
        }
        $this->ajaxReturn($arr);
    }
    public function attrEdit(){
       $data=M('attr')->where(array('id'=>I('get.id')))->find();
       $this->ajaxReturn($data);
    }
    public function doattrEdit(){      
        $data['type_id']=I('get.type_id');
        $data['attr_name']=I('get.attr_name');
        $data['def_att_val']=I('get.def_att_val');  
       $re=M('attr')->where(array('id'=>I('get.id')))->filter('strip_tags')->save($data);
        if($re){
            $arr['status']=1;
            $arr['msg']='修改成功';
        }else{
            $arr['status']=0;
            $arr['msg']='修改失败';
        }
       $this->ajaxReturn($arr);
    }
    
    //商品属性
    public function goods_attr(){
        $pagesize=4;
        $counts=M('goods_attr_g_t')->count();
        $pagetotal=ceil($counts/$pagesize);
        $data=M('goods')->field('id,goods_name')->select();
        $this->assign('goods',$data);
        $attr=M('attr_type')->select();
        $this->assign('attrs',$attr);
        $this->assign('pagetotal',$pagetotal);
        $this->display();
    }
    public function dogoods_attr(){
        $data=M('goods_attr_g_t')->page(I('get.p'),4)->select();
        $this->ajaxReturn($data);
    }
    public function dogoodsattradd(){
        $re=M('goods_attr')->filter('strip_tags')->add(I('get.'));
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='添加成功';
        }else{
            $arr['status']=0;
            $arr['msg']='添加失败';
        }
        $this->ajaxReturn($arr);
        
    }
    public function goodsattredit(){
        $re=M('goods_attr_g_t')->where(array('id'=>I('get.id')))->find();
        if($re){
            $this->ajaxReturn($re);
        }else{
            $arr['msg']='请求失败';
            $this->ajaxReturn($arr);
        }     
    }
    public function dogoodsattredit(){
       $data['attr_id']=I('get.attr_id');
       $data['attr_val']=I('get.attr_val');
        $re=M('goods_attr')->filter('strip_tags')->where(array('id'=>I('get.id')))->setField($data);
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='修改成功';
            $arr['attr']=M('goods_attr_g_t')->filter('strip_tags')->field('attr_name,attr_val,type_name')->where(array('id'=>I('get.id')))->find();           
        }else{
            $arr['status']=0;
            $arr['msg']='修改失败';    
        }
        $this->ajaxReturn($arr);
    }
    
    public function dogoodsattrdel(){
       $re= M('goods_attr')->where(array('id'=>I('get.id')))->delete();
       $arr=array();
       if($re){
           $arr['status']=1;
           $arr['msg']='删除成功';
       }else{
           $arr['status']=0;
           $arr['msg']='删除失败';
       }
       $this->ajaxReturn($arr);
        
    }
    
    //商品分类列表
    public function catelist(){
        $pagesize=4;
        $counts=M('cate')->count();
        $pagetotal=ceil($counts/$pagesize);
        $data=M('cate_type')->order('fid asc')->select();
        $list=BaseController::tree($data);
        $types=M('type')->select();
        $this->assign('type',$types);    
        $this->assign('types',$list);
        $cate=M('cate')->where(array('fid'=>0))->select();
        $this->assign('cate',$cate);
        $this->display();
    } 
    public function delcate(){
          $re= M('cate')->field('fid')->where(array('fid'=>I('get.id')))->find();
          $arr=array();
         if($re){
             $arr['status']=0;
             $arr['msg']='存在下级分类,请先删除分类'; 
             $this->ajaxReturn($arr);
             exit;          
         }
         $res= M('goods')->field('cate_id')->where(array('cate_id'=>I('get.id')))->find();
         if($res){
             $arr['status']=0;
             $arr['msg']='此分类存在商品,请先删除商品';
             $this->ajaxReturn($arr);
             exit; 
         }
          $s=M('cate')->where(array('id'=>I('get.id')))->delete();
          if($s){
             $arr['status']=1;
             $arr['msg']='删除成功';
             $this->ajaxReturn($arr);
          }
    } 
    public function docateadd(){
         $re=M('cate')->filter('strip_tags')->add(I('get.'));
         $arr=array();
         if($re){
             $arr['status']=1;
             $arr['msg']='添加成功';
             $arr['type']=M('cate_type')->where(array('id'=>$re))->find();
         }else{
             $arr['status']=0;
             $arr['msg']='添加失败';
         }
         $this->ajaxReturn($arr);
         
    }
    public function editcate(){
        $data=M('cate')->where(array('id'=>I('get.id')))->find();
        $this->ajaxReturn($data);
    }
    public function docateEdit(){
        if(I('get.id')==I('get.fid')){
           $data['cate_name']=I('get.cate_name');
           $data['type_id']=I('get.type_id');
           $data['cate_desc']=I('get.cate_desc');
           $re=M('cate')->where(array('id'=>I('get.id')))->setField($data); 
           if($re){
               $arr['status']=1;
               $arr['msg']='修改成功';
               $this->ajaxReturn($arr);
               exit;
           } else{
               $arr['status']=0;
               $arr['msg']='修改失败';
               $this->ajaxReturn($arr);
               exit;
           }    
        }
        $re=M('cate')->field('fid')->where(array('fid'=>I('get.id')))->find(); 
         $arr=array();
         if($re){
             $arr['status']=0;
             $arr['msg']='存在下级分类无法修改分类等级';
             $this->ajaxReturn($arr);
             exit();
         }
       $foo= M('goods')->field('cate_id')->where(array('cate_id'=>I('get.id')))->find();
         if($foo){
             $arr['status']=0;
             $arr['msg']='此分类存在商品,无法进行修改分类';
             $this->ajaxReturn($arr);
             exit; 
         }else{
            $s=M('cate')->filter('strip_tags')->save(I('get.'));
             if($s){
               $arr['status']=1;
               $arr['msg']='修改分类成功';             
               $this->ajaxReturn($arr);
             }  
         }
    }
    
    //商品类型
    public function type(){
        $pagesize=2;
        $counts=M('type')->count();
        $pagetotal=ceil($counts/$pagesize);
        $this->assign('pagetotal',$pagetotal);
        $this->display();
    }
    public function dotype(){
        $data=M('type')->page(I('get.p'),2)->select();
        $this->ajaxReturn($data);
    }
    public function typeEdit(){
        $data= M('type')->where(array('id'=>I('get.id')))->find();
        $this->ajaxReturn($data);
    }
    public function dotypeEdit(){
        $id=I('get.id');
        $data['type_name']=I('get.type_name');
        $data['desc']=I('get.desc');
        $re=M('type')->where(array('id'=>$id))->filter('strip_tags')->save($data);
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='编辑成功';
        }else{
            $arr['status']=0;
            $arr['msg']='编辑失败';
        }
        $this->ajaxReturn($arr);   
    }
    public function dotypeadd(){
        if(I('get.type_name')==''){
            $arr['status']=0;
            $arr['msg']='数据不能为空';
            $this->ajaxReturn($arr); 
            exit;
        }
       $data['type_name']=I('get.type_name');
       $re=M('type')->filter('strip_tags')->add($data);
        if($re){
            $arr['status']=1;
            $arr['msg']='添加成功';
        }else{
            $arr['status']=0;
            $arr['msg']='添加失败';
        }
        $this->ajaxReturn($arr);
    }
    public function typeDel(){
        $re=M('type')->where(array('id'=>I('get.id')))->delete();
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='删除成功';
        }else{
            $arr['status']=0;
            $arr['msg']='删除失败';
        }
        $this->ajaxReturn($arr);
    }
    //商品品牌
    public function  brand(){
       $pagesize=2;
       $counts=M('brand')->count();
       $pagetotal=ceil($counts/$pagesize);
       $this->assign('pagetotal',$pagetotal);
       $this->display();  
    }
    public function dobrand(){
         $data=M('brand')->page(I('get.p'),2)->select();
         $this->ajaxReturn($data);
    } 
    public function brandEdit(){
       $data= M('brand')->where(array('id'=>I('get.id')))->find();
       $this->ajaxReturn($data);
    }
    public function dobrandEdit(){
        $id=I('get.id');
        $data['brand_name']=I('get.brand_name');
        $data['desc']=I('get.desc');
        $re=M('brand')->where(array('id'=>$id))->filter('strip_tags')->save($data);
        $arr=array();
        if($re){
            $arr['status']=1;
            $arr['msg']='编辑成功';
        }else{
            $arr['status']=0;
            $arr['msg']='编辑失败';
        }
        $this->ajaxReturn($arr); 
    }
    public function brandDel(){
        $re=M('brand')->where(array('id'=>I('get.id')))->delete();
        $arr=array();
        if($re){
           $arr['status']=1;
           $arr['msg']='删除成功'; 
        }else{
           $arr['status']=0;
           $arr['msg']='删除失败';
        }
        $this->ajaxReturn($arr);
    }
    public function dobrandadd(){
        if(I('get.brand_name')=='' || I('get.desc')=='' ){
            $arr['status']=0;
            $arr['msg']='数据不能为空';
            $this->ajaxReturn($arr);
            exit;
        }
        //判断商品品牌是否为空
       $row=M('brand')->field('brand_name')->filter('strip_tags')->where(array('brand_name'=>I('get.brand_name')))->find();
       $arr=array();
        if($row){
            $arr['status']=0;
            $arr['msg']='品牌名称重复';
            $this->ajaxReturn($arr);
            exit;
        }
        $data=I('get.');
        $re=M('brand')->filter('strip_tags')->add($data);
        
        if($re){
           $arr['status']=1;
           $arr['msg']='添加成功'; 
        }else{
           $arr['status']=0;
           $arr['msg']='添加失败';
        }
        $this->ajaxReturn($arr);
    }
<<<<<<< .mine
   
||||||| .r96
    
=======
    //商品评论管理
    public function manger(){
        $data=M('comment_goods')->select();
        $this->assign('comments',$data);
        $this->display();
    }
    //敏感词过滤
    public function dopass(){
     $re= M('comment')->where(array('id'=>I('get.id')))->setField('check',1);
       $arr=array();
        if($re){
           $arr['status']=1;
           $arr['msg']='审核通过'; 
        }else{
           $arr['status']=0;
           $arr['msg']='审核失败';
        }
        $this->ajaxReturn($arr);
    }
    public function delcomment(){
        $re= M('comment')->where(array('id'=>I('get.id')))->delete();
        $res=M('comment')->where(array('fid'=>I('get.id')))->delete();
        $arr=array();
        if($re&&$res){
            $arr['status']=1;
            $arr['msg']='删除评论成功';
        }else{
            $arr['status']=0;
            $arr['msg']='删除评论失败';
        }
        $this->ajaxReturn($arr);
    }
 
    
>>>>>>> .r312
    
    
    
}